package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.UserEntity;
import service.TestService;
import service.impl.UserServiceImpl;

public class TestServlet extends HttpServlet {
	static TestService serviceOne = new UserServiceImpl();
	private static String user;

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		arg0.setCharacterEncoding("UTF-8");
		arg1.setCharacterEncoding("UTF-8");
		String method = arg0.getParameter("method");
		System.out.println(method);
		if ("add".equals(method)) {
			add(arg0, arg1);
			list(arg1);
		} else if ("update".equals(method)) {
			update(arg0, arg1);
			list(arg1);
		} else if ("delete".equals(method)) {
			delete(arg0);
			list(arg1);
		} else if ("toUpdate".equals(method)) {
			toUpdate(arg0, arg1);
		} else if ("List".equals(method)) {
			list(arg1);
		} else {
			list(arg1);
		}
	}

	private void add(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println(req.getParameter("user"));
		System.out.println("update");
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String phone = req.getParameter("phone");
		UserEntity userEntity = new UserEntity();
		userEntity.setUserName(userName);
		userEntity.setPassword(password);
		userEntity.setPhone(phone);
		serviceOne.add(userEntity);
	}

	private void update(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		System.out.println(req.getParameter("user"));
		System.out.println("update");
		Long id = Long.valueOf(req.getParameter("id"));
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String phone = req.getParameter("phone");
    	UserEntity userEntity = new UserEntity();
		userEntity.setId(id);
		userEntity.setUserName(userName);
		userEntity.setPassword(password);
		userEntity.setPhone(phone);
		serviceOne.update(userEntity);
	}

	private void toUpdate(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
//		UserEntity userEntity = new UserEntity();
		UserEntity userEntity = serviceOne.getById(id);
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		out.println("<form action=\"DongServlet?method=update\"method=\"post\">");
		out.println("<input type='hidden' name='id' value='" + userEntity.getId() + "'><p>");
		out.println("<input name='userName' value='" + userEntity.getUserName() + "'><p>");
		out.println("<input name='password' value='" + userEntity.getPassword() + "'><p>");
		out.println("<input name='phone' value='" + userEntity.getPhone() + "'><p>");
		out.println("<input name='creationTime value='" + userEntity.getCreationTime() + "'><p>");
		out.println("<input name='updateTime' value='" + userEntity.getUpdateTime() + "'><p>");
		out.println("<input name='deletelogo' value='"+ userEntity.getDeleteLogo()+"'><p>");
		out.println("<input type=\"submit\" value=\"提交\">\r\n" + "</form>");
	}

	private void delete(ServletRequest req) {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
		serviceOne.delete(id);
	}

	private void list(ServletResponse response) throws IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<h1>" + user + "</h1>");
		out.println("<a href='servletLei/lei.html'>添加</a>");
		out.println("<table border=1>");
		out.println("<tr><td>id</td><td>姓名</td><td>密码</td><td>电话</td><td>创建时间</td><td>修改时间</td><td>删除标识</td><td>操作</td></tr>");
		List<UserEntity> userList = serviceOne.listAll();
		for (UserEntity us : userList) {
			if ("Y".equals(us.getDeleteLogo())) {
				continue;
			}
			out.println("<tr>");
			out.println("<td>"+us.getId()+"</td>");
			out.println("<td>"+us.getUserName()+"</td>");
			out.println("<td>"+us.getPassword()+"</td>");
			out.println("<td>"+us.getPhone()+"</td>");
			out.println("<td>"+us.getCreationTime()+"</td>");
			out.println("<td>"+us.getUpdateTime()+"</td>");
			out.println("<td>"+us.getDeleteLogo()+"</td>");
			out.println("<td>");
			out.println("<a href='DongServlet?method=delete&id=" + us.getId() + "'>删除</a>    ");
			out.println("<a href='DongServlet?method=toUpdate&id=" + us.getId() + "'>修改</a>");
			out.println("</td>");
			out.println("</tr>");
		}
		out.println("</table>");
	}

	@Override
	public void destroy() {
		System.out.println("servlet销毁.....");
		super.destroy();
	}

}
